package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.ItemSku;
import com.armani.model.Result;
import com.armani.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SkuController
 * @Description 商品sku控制层
 * @Author Admin
 * @Date 2021/11/27 17:51
 * @Version 1.0
 **/
@Controller
@RequestMapping("/sku")
public class SkuController {
    //自动装配

    @Autowired
    private SkuService skuService;
    @GetMapping(value ="/listSearch.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String pageSearch(@RequestParam Integer page,
                              @RequestParam Integer limit,
                              @RequestParam(required = false) Integer id){
        Integer startRow=(page-1)*limit;
        List<ItemSku> skus =skuService.pageSearch(startRow, limit,id);
        Integer skuCount=skuService.getCount(id);
        Result result=new Result();
        result.setCount(skuCount);
        result.setData(skus);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping(value ="/insert.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String insert( @RequestParam String title,
                           @RequestParam String descrip,
                           @RequestParam Integer spuId,
                           @RequestParam BigDecimal price,
                           @RequestParam String param,
                           @RequestParam Integer inventory,
                           @RequestParam Integer enable,
                           @RequestParam  Date creatTime,
                           @RequestParam Date lastTime){
        Integer insert=skuService.insert(title, descrip, spuId, price, param,inventory, enable, creatTime, lastTime);
        Result result=new Result();
        result.setData(insert);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @RequestMapping(value ="/update.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String update(@RequestParam(required = false) Integer id,
                          @RequestParam(required = false) String title,
                          @RequestParam(required = false) String descrip,
                          @RequestParam(required = false) Integer spuId,
                          @RequestParam(required = false) BigDecimal price,
                          @RequestParam(required = false) String param,
                          @RequestParam(required = false) Integer inventory,
                          @RequestParam(required = false) Integer enable,
                          @RequestParam(required = false)  Date createTime,
                          @RequestParam(required = false) Date lastTime){
        Integer update=skuService.update(id,title, descrip, spuId, price, param,inventory, enable, createTime, lastTime);
        Result result=new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @RequestMapping(value ="/delete.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String delete(@RequestParam Integer id){
        Integer update=skuService.delete(id);
        Result result=new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
}
